package com.tocea.formation.tpjuni4;

import java.util.Collection;
import java.util.Iterator;

public class Set implements ISet{

	public Integer tab[];
	
	public Set(int n){
		tab = new Integer[n];
	}
	
	public int size() {
		return tab.length;
	}

	public boolean isEmpty() {
		return  tab.length==0;
	}

	public boolean contains(Integer o) {
		for(int i=0; i<tab.length; i++){
			if(tab[i].equals(o)){
				return true;
			}
		}
		return false;
	}

	public boolean add(Integer e) {
	    Integer temp[]= new Integer[tab.length+1];
	    System.arraycopy(tab, 0, temp, 0, tab.length);
	    temp[tab.length]= e;
	    tab= temp;
		return true;
	}

	public boolean remove(Object o) {
	    Integer temp[]= new Integer[tab.length-1];
	    int cpt=0;
	    for(int i=0; i<tab.length; i++){
	    	if(!tab[i].equals(o)){
	    		temp[cpt]=tab[i];
	    		cpt++;
	    	}
	    }
	    tab= temp;
		return true;
	}

	public boolean addAll(Collection<Integer> c) {
		Iterator<Integer> itr = c.iterator(); 
		while(itr.hasNext()) {
		    this.add(itr.next()); 
		}
		return true;
	}

	public boolean removeAll(Collection<Integer> c) {
		Iterator<Integer> itr = c.iterator(); 
		while(itr.hasNext()) {
		    this.remove(itr.next()); 
		}
		return true;
	}

	public void clear() {
		tab= new Integer[0];
	}

}
